package edu.uta.BillMeApp;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import edu.BillME.datapars.HttpConnection;
import edu.uta.BillMeApp.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
//import android.util.Log;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/* @auther Siddharth Patel & Susan Foster */
public class LoginPage extends Activity {
	EditText un,pw;
	TextView result;
	Button ok, register;

	private void next(){
		String welcomeName=un.getText().toString();
		Bundle bundle = new Bundle();
		bundle.putString("wN",welcomeName);
		Intent nextPage = new Intent(LoginPage.this,TableOfCont.class);
		nextPage.putExtras(bundle);
		startActivity(nextPage);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		un=(EditText)findViewById(R.id.username);
		pw=(EditText)findViewById(R.id.password);
		ok=(Button)findViewById(R.id.login_button);
		result=(TextView)findViewById(R.id.result);
		register=(Button)findViewById(R.id.register);
		
		ok.setOnClickListener(new View.OnClickListener(){

			//@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
				postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

				String response = null;
				try {
					response = HttpConnection.executeHttpPost("http://omega.uta.edu/~smp3753/checklogin.php", postParameters);
					String res=response.toString();
					res= res.replace("\n","");
					if(res.equals("1")){
						next();
						//result.setText("Correct Username or Password");
					}
					else{
						result.setText("Sorry!! Incorrect Username or Password");
					}

				}catch (Exception e) {
					Log.w("exception",e.toString());
				}
			}
		});
		
		register.setOnClickListener(new View.OnClickListener() {
			
			//	@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub
					Intent IntentRegister = new Intent(view.getContext(),Register.class);
					Log.d("register button", "its working");
					startActivity(IntentRegister);
					Log.d("register button", "its working");
				}
			});
	}//end onCreate
}//end class